package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.CommissionDepositInfo;

import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/4/3.
 * 佣金收益提现记录
 */

public class CommissionDepositDetialFragment extends CommissionDepositDetialRefreshFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    private List<CommissionDepositInfo> infoList;
    private String state,way;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new CommissionDepositAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;
        Bundle args = getArguments();
        state = args.getString("state");
        if (state.equals("commission")) {
            way = "yj";
        } else if (state.equals("principal")) {
            way = "bj";
        }
    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        CommissionDepositInfo info = (CommissionDepositInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
    }

    public static class CommissionDepositAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_deposit_detial,
                                                                  parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;

            CommissionDepositInfo info = (CommissionDepositInfo) _data.get(position);
            vh.tv_DaoZhang.setText(info.getPaymoney());
            if (info.getPayflag() == 0) {
                vh.tv_state.setText("审核中");
            } else if (info.getPayflag() == 1) {
                vh.tv_state.setText("审核通过");
            } else if (info.getPayflag() == 2) {
                vh.tv_state.setText("审核未通过");
            }
            if (!TextUtils.isEmpty(info.getChecktime())) {
                vh.tv_time.setText(DateUtils.getDateToStringDay(info.getChecktime()));
            }
        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_DaoZhang)
            TextView tv_DaoZhang;
            @BindView(R.id.tv_state)
            TextView tv_state;
            @BindView(R.id.tv_time)
            TextView tv_time;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.commissionDepositRecord(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                              way, new JsonCallback<CommissionDepositInfo>(
                        CommissionDepositInfo.class) {

                    @Override
                    public void onError(Call call, Exception e) {
                        onRequestError();
                    }

                    @Override
                    public void onResponse(ResponseInfo<CommissionDepositInfo> response) {
                        int code = response.getCode();
                        if (code == 1) {
                            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                            return;
                        }
                        infoList = response.getData();
                        if (infoList != null) {
                            Collections.sort(infoList,
                                    new Comparator<CommissionDepositInfo>() {
                                        @Override
                                        public int compare(
                                                CommissionDepositInfo commissionDepositInfo,
                                                CommissionDepositInfo t1
                                        ) {
                                            return commissionDepositInfo.getChecktime().compareTo(
                                                    t1.getChecktime());
                                        }
                                    }
                            );
                        }
                        onRequestResponse(infoList);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        onRequestFinish();
                    }
                });
    }

}
